import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Leet841 {
    static Set<Integer> set,guo;
    static public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        set = new HashSet<>();guo = new HashSet<>();
        dfs(rooms,0);
        if (!set.contains(0))
             return set.size()==rooms.size()-1;
        else return set.size()==rooms.size();
    }
    static void dfs(List<List<Integer>> rooms, int now){
        guo.add(now);
        set.addAll(rooms.get(now));
        for (Integer integer : new ArrayList<>(set))
            if (!guo.contains(integer))
                dfs(rooms,integer);
    }
}
